# -*- coding: UTF-8 -*-
# 作者： 磊
# @Time：2022/5/4
# coding=utf-8
import json
import random

import allure
import pytest as pytest
import requests
class TestSecondRequest2:
    access_token=""
    def test_get_toke(self):
        url="https://api.weixin.qq.com/cgi-bin/token"
        params={
            'appid': 'wx74a8627810cfa308',
            'secret': 'e40a02f9d79a8097df497e6aaf93ab80',
            'grant_type': 'client_credential'
        }
        req=requests.get(url=url,params=params)
        return_data=req.json()

        # 返回字典格式数据
        print("返回字典格式数据：",return_data)
        TestSecondRequest2.access_token=return_data['access_token']

    def test_post_edit(self):
        url="https://api.weixin.qq.com/cgi-bin/tags/update?access_token="+TestSecondRequest2.access_token+""
        print(url)
        data = {"tag":{"id":108,"name":"ms"+str(random.randint(10000,99999))}}
        #application/x-www-form-urlencoded   data传参  这里是嵌套的，不行
        #application/json                    json传参
        # json.dumps()  把json格式转换成字符串
        # json.loads()  把字符串转换成字典
        res = requests.post(url=url, json=data) # 这个的data可以转换成json.dumps(data)，在通过data进行传参
        print("请求头：",res.request.headers)
        print(res.json())

    def test_file_up(self):
        url="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="+TestSecondRequest2.access_token+""
        print(url)
        data={
            "media":open(r"E:\shu.png","rb")
        }
        res=requests.post(url,files=data)
        print("请求头：", res.request.headers)
        print(res.json())

if __name__ == '__main__':
        pytest.main(['-vs',"test02.py"])




